// 注册页面
const app = getApp();

Page({
  data: {
    username: '',
    password: '',
    confirmPassword: '',
    nickname: '',
    phone: '',
    isLoading: false,
    showPassword: false,
    showConfirmPassword: false,
    passwordMatch: null,
    passwordTips: '',
    passwordStrength: 0
  },
  
  // 输入用户名
  inputUsername(e) {
    this.setData({
      username: e.detail.value
    });
  },
  
  // 输入密码
  inputPassword(e) {
    const password = e.detail.value;
    this.setData({
      password
    });
    
    this.calculatePasswordStrength(password);
    
    this.checkPasswordMatch(password, this.data.confirmPassword);
  },
  
  // 确认密码
  inputConfirmPassword(e) {
    const confirmPassword = e.detail.value;
    this.setData({
      confirmPassword
    });
    
    this.checkPasswordMatch(this.data.password, confirmPassword);
  },
  
  calculatePasswordStrength(password) {
    let strength = 0;
    
    if (!password) {
      this.setData({ passwordStrength: 0 });
      return;
    }
    
    if (password.length >= 8) {
      strength += 1;
    }
    
    const hasLetter = /[a-zA-Z]/.test(password);
    const hasNumber = /[0-9]/.test(password);
    const hasSpecial = /[^a-zA-Z0-9]/.test(password);
    
    if ((hasLetter && hasNumber) || (hasLetter && hasSpecial) || (hasNumber && hasSpecial)) {
      strength += 1;
    }
    
    if (hasLetter && hasNumber && hasSpecial) {
      strength += 1;
    }
    
    this.setData({ passwordStrength: strength });
  },
  
  checkPasswordMatch(password, confirmPassword) {
    if (!password || !confirmPassword) {
      this.setData({
        passwordMatch: null,
        passwordTips: ''
      });
      return;
    }
    
    if (password === confirmPassword) {
      this.setData({
        passwordMatch: true,
        passwordTips: '密码一致'
      });
    } else {
      this.setData({
        passwordMatch: false,
        passwordTips: '两次密码不一致'
      });
    }
  },
  
  togglePasswordVisibility() {
    this.setData({
      showPassword: !this.data.showPassword
    });
  },
  
  toggleConfirmPasswordVisibility() {
    this.setData({
      showConfirmPassword: !this.data.showConfirmPassword
    });
  },
  
  // 输入昵称
  inputNickname(e) {
    this.setData({
      nickname: e.detail.value
    });
  },
  
  // 输入手机号
  inputPhone(e) {
    this.setData({
      phone: e.detail.value
    });
  },
  
  // 注册
  register() {
    const { username, password, confirmPassword, nickname, phone } = this.data;
    
    // 表单验证
    if (!username) {
      wx.showToast({
        title: '请输入用户名',
        icon: 'none'
      });
      return;
    }
    
    if (!password) {
      wx.showToast({
        title: '请输入密码',
        icon: 'none'
      });
      return;
    }
    
    if (password !== confirmPassword) {
      wx.showToast({
        title: '两次密码不一致',
        icon: 'none'
      });
      return;
    }
    
    // 设置加载状态
    this.setData({
      isLoading: true
    });
    
    // 调用注册接口
    app.globalData.api.user.add({
      username,
      password,
      nickname,
      phone
    }).then(res => {
      // 注册成功
      wx.showToast({
        title: '注册成功',
        icon: 'success'
      });
      
      // 返回登录页
      setTimeout(() => {
        wx.navigateBack();
      }, 1500);
    }).catch(err => {
      console.error('注册失败', err);
    }).finally(() => {
      // 取消加载状态
      this.setData({
        isLoading: false
      });
    });
  },
  
  // 返回登录页
  goLogin() {
    wx.navigateBack();
  }
}) 